package org.jcb.parser ;

import java.io.File;




public class ParserFiles {
ITraiteFichier changeFile ;
IFilterFileParser filter ;
	
public IFilterFileParser getFilter() {
	return filter;
}

public void setFilter(IFilterFileParser filter) {
	this.filter = filter;
}

public ITraiteFichier getChangeFile() {
	return changeFile;
}

public void setChangeFile(ITraiteFichier changeFile) {
	this.changeFile = changeFile;
}

	public void parseDirectory(File dir) {
		String[] children = dir.list();
		if (children != null) {
		    for (int i=0; i<children.length; i++) {
		        String filename = dir.getAbsolutePath() + "/" + children[i];
		        File file = new File( filename);
		        System.out.println(filename);
		        if (file.isDirectory() && filter.isDirectoryOK(file)) {
		        	parseDirectory(file);
		        }
		        else {
		        	if (filter.isFileOK(file)) {
		        		changeFile.traiteFichier(file);
		        	}
		        }		        
		    }
		}	
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		File dir = new File(args[0]);
		ParserFiles parser = new ParserFiles();
		Class<? extends ITraiteFichier> c = null;
		Class<? extends IFilterFileParser> cFilter = null;
		ITraiteFichier changeFile = null;
		IFilterFileParser filter = null ;
		String classTrtFile = args[1]  ; //"jcb.FileCp1252ToIso88591"
		String classFilter = args[2]  ; //"jcb.FilterProjetCp1252ToIso"
		
		try {
			c = Class.forName(classTrtFile).asSubclass(ITraiteFichier.class);
			cFilter = Class.forName(classFilter).asSubclass(IFilterFileParser.class);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		try {
			changeFile = c.newInstance();
			filter = cFilter.newInstance();
			parser.setChangeFile(changeFile);
			parser.setFilter(filter);			
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		

		parser.parseDirectory(dir);

	}


	
}
